import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/commom/user.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_module/event/logout_event.dart';
import 'package:flutter_module/pages/login/repassword.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:flutter_module/commom/application.dart';
import 'package:flutter_module/commom/user.dart';
import 'package:flutter_module/event/login_event.dart';
import 'package:flutter_module/http/api_service.dart';
import 'package:flutter_module/http/UserModel.dart';
import 'package:flutter_module/pages/rumor/rumor_page.dart';
import 'package:flutter_module/util/bubble_indication_painter.dart';
import 'package:flutter_module/util/theme_util.dart';
import 'package:flutter_module/commom/user.dart';
import 'package:flutter_module/config/api.dart';
import 'package:flutter_module/http/dio_manager.dart';
import 'package:flutter_module/pages/login/login_page.dart';
import 'package:share/share.dart';
import 'package:flutter_module/mqtt/Mqtt.dart';

import '../../DemoLocalizations.dart';
import '../connwifi.dart';

class DrawerPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new DrawerPageState();
  }
}

class DrawerPageState extends State<DrawerPage> {
  bool isLogin = false;
  String username = "未登录";

  @override
  void initState() {
    super.initState();
    this.registerLoginEvent();
  }

  void registerLoginEvent() {
    Application.eventBus.on<LoginEvent>().listen((event) {
      changeUI();
    });
  }

  changeUI() async {
    if (mounted) {
      setState(() {
        isLogin = true;
        username = User.singleton.userName;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    setState(() {
      username = DemoLocalizations.of(context).notLogin;
    });
    if (null != User.singleton.userName) {
      isLogin = true;
      username = User.singleton.userName;
    }

    var stack = new Stack(
      alignment: const FractionalOffset(0.5, 0.89),
      children: <Widget>[
        new CircleAvatar(
          backgroundImage: new NetworkImage(
              'http://b-ssl.duitang.com/uploads/item/201805/13/20180513224039_tgfwu.png'),
          radius: 66.0,
        ),
        new Container(
          decoration: new BoxDecoration(
              // color: Colors.pinkAccent,
              ),
          padding: EdgeInsets.fromLTRB(20.0, 25.0, 20.0, 5.0),
          child: new Text(username),
        )
      ],
    );

    var stack2 = new Stack(
      alignment: const FractionalOffset(0.5, 0.89),
      children: <Widget>[
        new CircleAvatar(
          backgroundImage: AssetImage('assets/images/user_icon.png'),
          radius: 66.0,
        ),
      ],
    );

    return Drawer(
      child: Container(
        decoration: BoxDecoration(color: Colors.white),
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
                decoration: BoxDecoration(
                  color: Colors.lightBlue,
                ),
                child: Center(
                  child: GestureDetector(
                    onTap: () {
                      // print("点击事件");
                      if (!isLogin) {
                        Navigator.of(context)
                            .push(new MaterialPageRoute(builder: (context) {
                          return new LoginPage();
                        }));
                      }
                    },
                    child: SizedBox(
                      width: 130.0,
                      height: 130.0,
                      child: CircleAvatar(
                        child: Text(username),
                      ),
                    ),
                  ),
                )),
            // Center(child: stack2),
            // ignore: missing_required_param
            // UserAccountsDrawerHeader(
            //   accountName: InkWell(
            //     child: Text(username,
            //         style: TextStyle(
            //           fontWeight: FontWeight.bold,
            //           fontSize: 20,
            //         ),
            //         textAlign: TextAlign.center),
            //     onTap: () {
            //       if (!isLogin) {
            //         Navigator.of(context)
            //             .push(new MaterialPageRoute(builder: (context) {
            //           return new LoginPage();
            //         }));
            //       }
            //     },
            //   ),
            //   currentAccountPicture: InkWell(
            //     child: CircleAvatar(
            //       backgroundImage: AssetImage('assets/images/user_icon.png'),
            //       // backgroundImage: NetworkImage("http://img8.zol.com.cn/bbs/upload/23765/23764201.jpg"),
            //     ),
            //     onTap: () {
            //       if (!isLogin) {
            //         Navigator.of(context)
            //             .push(new MaterialPageRoute(builder: (context) {
            //           return new LoginPage();
            //         }));
            //       }
            //     },
            //   ),
            // ),
            new Card(
              child: ListTile(
                title: Text(
                  DemoLocalizations.of(context).changePassword,
                  textAlign: TextAlign.left,
                ),
                trailing: new Icon(Icons.arrow_forward_ios),
                contentPadding: EdgeInsets.symmetric(horizontal: 20.0),
                leading: Icon(FontAwesomeIcons.lock, size: 22.0),
                onTap: () {
                  Navigator.of(context)
                      .push(new MaterialPageRoute(builder: (context) {
                    return new repassword();
                  }));
                },
              ),
            ),

            new Card(
              child: new Visibility(
                visible: isLogin,
                child: ListTile(
                  title: Text(
                    DemoLocalizations.of(context).myShare,
                    textAlign: TextAlign.left,
                  ),
                  leading: Icon(Icons.share, size: 22.0),
                  trailing: new Icon(Icons.arrow_forward_ios),
                  contentPadding: EdgeInsets.symmetric(horizontal: 20.0),
                  onTap: () {
                    if (isLogin) {
                      onCollectionClick();
                    } else {
                      onLoginClick();
                    }
                  },
                ),
              ),
            ),
            new Card(
              child: new Visibility(
                visible: isLogin,
                child: ListTile(
                  title: Text(
                    DemoLocalizations.of(context).addDevice,
                    textAlign: TextAlign.left,
                  ),
                  leading: Icon(Icons.add_box, size: 22.0),
                  trailing: new Icon(Icons.arrow_forward_ios),
                  contentPadding: EdgeInsets.symmetric(horizontal: 20.0),
                  onTap: () {
                    Navigator.of(context)
                        .push(new MaterialPageRoute(builder: (context) {
                      return new Connwifi();
                    }));
                  },
                ),
              ),
            ),
            new Card(
              child: logoutWidget(),
            )
          ],
        ),
      ),
    );
  }

  Widget logoutWidget() {
    if (User.singleton.userName != null) {
      return ListTile(
        title: Text(
          DemoLocalizations.of(context).loginOut,
          textAlign: TextAlign.left,
        ),
        leading: Icon(Icons.exit_to_app, size: 22.0),
        trailing: new Icon(Icons.arrow_forward_ios),
        contentPadding: EdgeInsets.symmetric(horizontal: 20.0),
        onTap: () {
          User.singleton.clearUserInfor();
          Application.eventBus.fire(new LogoutEvent());
          setState(() {
            isLogin = false;
            username = DemoLocalizations.of(context).notLogin;
          });
        },
      );
    } else {
      return SizedBox(
        height: 0,
      );
    }
  }

  void onCollectionClick() async {
    await Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return new RumorPage();
    }));
  }

  void onLoginClick() async {
    await Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return new LoginPage();
    }));
  }
}
